package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.custom.AjaxResult;
import com.example.demo.entity.SysUser;
import com.example.demo.form.PwdForm;
import com.example.demo.security.UserUtils;
import com.example.demo.service.SysUserService;

@RestController
@RequestMapping(value = "/pwd")
public class SetPasswordController {
	
	@Autowired
	private SysUserService sysUserService;
	
	@Autowired 
	private UserUtils userUtils;

	@RequestMapping(value = "/setPwd")
	@ResponseBody
	public Object setPwd(PwdForm form){	
		String password1 = form.getPass();
		String password2 = form.getCheckPass();
		String password3 = form.getPass2();
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
		SysUser user = userUtils.getUser();
		String password = user.getPassword();
		if(encoder.matches(password1, password)) {
			if(password2.equals(password3)) {
				String encode = encoder.encode(password2);
				user.setPassword(encode);
				sysUserService.save(user);
				return new AjaxResult("修改成功");
			}
		}
		return new AjaxResult(false,"旧密码不正确");
	}
}
